{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **演示0105：数组排序**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例1：一维数组排序**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **升序和降序排列**  \n",
    ">* *sort*只能升序排列，如果需要降序，可以再倒序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 7 8]\n",
      "[8 7 5 4 3 2 1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([3, 1, 7, 4, 2, 5, 8])\n",
    "b = np.sort(a)\n",
    "print(b)\n",
    "print(b[::-1])    # 使用[::-1]进行倒序遍历"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> ***np.sort(a)*和*a.sort*的不同行为**  \n",
    ">* *np.sort(a)*不修改a的值，而是返回一个拷贝，该拷贝是排序后的\n",
    ">* *a.sort()*返回None，它将直接在a上进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 1 7 4 2 5 8]\n",
      "[1 2 3 4 5 7 8]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([3, 1, 7, 4, 2, 5, 8])\n",
    "b = np.sort(a)\n",
    "print(a)\n",
    "a.sort()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例2：二维数组排序**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">**按行或案列排序**  \n",
    ">* 指定*axis=0*，表示沿竖直方向(行索引增加的方向)，对每一列分别排序\n",
    ">* 指定*axis=1*，表示沿水平方向(列索引增加的方向)，对每一行分别排序\n",
    ">* 未指定*axis*时，行为等同于*axis=1*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 3 4]\n",
      " [1 3 8]\n",
      " [0 2 7]]\n",
      "[[1 0 3]\n",
      " [2 1 7]\n",
      " [3 4 8]]\n",
      "[[1 3 4]\n",
      " [1 3 8]\n",
      " [0 2 7]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,4,3],[3,1,8], [2,0,7]])\n",
    "print(np.sort(a))\n",
    "print(np.sort(a, axis=0))\n",
    "print(np.sort(a, axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **案例3：数据分区**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">**根据某个边界数，将数组中的数据分成两个区，左区的值均小于边界数，右区的值均大于边界数**  \n",
    ">* $np.partition$函数用于执行分区操作。该函数指定边界元素的下标索引号，分区完成后，该下标索引号左边的元素值都小于该边界元素，右边的元素都大于该边界元素\n",
    ">* 左区和右区内部的数据并不会进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 20  80  70 100  50  40  30  90]\n",
      "[ 20  30  70  80  50  40  90 100]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([30, 80, 70, 100, 50, 40, 20, 90])\n",
    "b = np.partition(a, 0)    # 分区完成后，0号索引位置右边的元素都大于该位置元素值\n",
    "c = np.partition(a, 6)    # 分区完成后，6号索引位置左边的元素都小于该位置元素值；右边的元素都大于该位置元素值\n",
    "print(b)    # 0号索引位置元素值为20\n",
    "print(c)    # 6号索引位置元素值为90"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
